package com.fingard.dsp.bank.directbank.abc03;

import com.fingard.dsp.bank.directbank.abc02.ABC02Header;
import com.fingard.dsp.bank.directbank.abc02.ABC02TransCode;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.apache.commons.lang.StringEscapeUtils;

import java.util.Date;

public class ABC03DeContract extends ABC03Base implements IDeContract {
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try{
            XmlTextWriter xmlWriter = new XmlTextWriter();
            ABC02Header bankHeader = new ABC02Header();
            XmlTextWriter xmlWriterOut = new XmlTextWriter();
            bankHeader.CCTransCode = ABC02TransCode.deal_decontract;
            bankHeader.ReqSeqNo = p_req.contractDetail.askNumber;
            bankHeader.ReqDateTime = new Date();

            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("DbProv", getBankConfig().getMapValueByKey("dbProvCFRTC6", p_req.contractDetail.recAct));
            xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req.contractDetail.recAct));
            xmlWriter.writeElementString("DbCur", "01");
            xmlWriter.writeElementClosed("DbLogAccNo");
            xmlWriter.writeElementString("CrProv", p_req.contractDetail.payArea);
            xmlWriter.writeElementString("CrAccNo", getAbcAccNo(p_req.contractDetail.payAct));
            xmlWriter.writeElementString("CrCur", "01");
            xmlWriter.writeElementClosed("CrLogAccNo");
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementClosed("DbAccName");
            xmlWriter.writeElementClosed("CrAccName");
            xmlWriter.writeElementString("NFAccNo", getBankConfig().getMapValueByKey("nFAccNoCFRTC6", p_req.contractDetail.recAct));
            xmlWriter.writeElementString("NVoucherType", getBankConfig().getValueAsString("nVoucherTypeCFRTC6"));
            xmlWriter.writeElementString("ContPhone", p_req.contractDetail.cellPhone);
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("Acc");
            xmlWriter.writeElementString("AccType","1");
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            txCode = "ECIP000190";
            reqSeqID = p_req.contractDetail.askNumber;
            writeCommonHeader(xmlWriterOut);
            xmlWriterOut.writeElementString("Transaction_Body",StringEscapeUtils.unescapeXml(xmlWriter.toXmlString()));
            xmlWriterOut.writeEndDocument();//Transaction_Body
            String[] tmpStrRet = sendToBank(StringEscapeUtils.unescapeXml(xmlWriterOut.toXmlString()));
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String returnMsg = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/Transaction_Body");
                    XmlTextReader tmpBkReaders = loadBankRespXmlReader(returnMsg, tmpSbMsg);
                    if (tmpBkReaders != null) {
                        String tmpRespCode = tmpBkReaders.getFirstText("/ap/RespCode");
                        String tmpRetInfo = tmpBkReaders.getFirstText("/ap/RespInfo");
                        String tmpRetRxtInfo = tmpBkReaders.getFirstText("/ap/RxtInfo");
                        String tmpRespSource = tmpBkReaders.getFirstText("/ap/RespSource");
                        if (tmpRespSource != null && "0".equals(tmpRespSource) && StringHelper.hasAnyChar(tmpRespCode)) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = StringHelper.joinNotEmpty("+", tmpRetInfo, tmpRetRxtInfo);
                            if ("0000".equals(tmpRespCode)) {
                                tmpRetDetail.deSignState = ContractState.SUCCESS;
                            } else {
                                tmpRetDetail.deSignState = ContractState.FAIL;
                            }
                        } else if ("ESVCA0000".equals(tmpRespCode)) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = StringHelper.joinNotEmpty("+", tmpRetInfo, tmpRetRxtInfo);
                            tmpRetDetail.deSignState = ContractState.SUCCESS;
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpRetInfo;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}
